// 改用 axios 替换 fetch
const axios = require('axios');

let express = require('express');
let router = express.Router();

const userService = require('../service/userService');
const { verifyJwtToken } = require('../util/server_jwt');


/*
 * 微信用户登录
 * 用途: 根据小程序请求的用户临时登录凭证code, 向微信获取用户的 openid 和 session_key
 */
router.post('/wxLoginBycode', async function(req, res, next) {
    let code = req.body.code;
    if (!code) {
        return res.status(400).json({err: '临时凭证为空'});
    }
    let result = await userService.wxLoginBycode(code);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 检测用户登录
 */
router.post('/checkUserLoginByJwt', async function(req, res, next) {
    let loginToken = req.body.loginToken;
    let powerToken = req.body.powerToken;
    if (!loginToken || !powerToken) {
        return res.status(400).json({err: 'jwt为空'});
    }
    // 验证jwt
    let result = await userService.checkUserLoginByJwt(loginToken, powerToken, res);
    res.status(result.success ? 200 : 400).json(result);
});

// /*
//  * 手机号密码登录
//  * 用途: 根据手机号和密码, 登录
//  */
// router.post('/loginByPhone', async function(req, res, next) {
//     // 判空
//     if (!req.body || !req.body.phone || !req.body.password) {
//         res.status(400).json({err: '传入参数有误'});
//     }
//     // 准备登录数据
//     let loginData = {
//         phone: req.body.phone,
//         password: req.body.password
//     }
//     // 进行用户登录 - 获取用户的uid和openid
//     let result = await userService.loginByPhone(loginData);
//     if (!result.result) {
//         res.status(400).json({err: '用户名或密码错误'});
//     }
//     // 生成jwt令牌
//     let jwt_data = {uid: result.userInfo.uid, openid: result.userInfo.openid};
//     let jwt = await createUserJwtToken(jwt_data, {expiresIn: '720h'});
//     // 响应jwt令牌
//     res.json({jwt: jwt});
// });


/*
 * 获取用户个人信息
 */
router.post('/getUserInfoByJwt', async function(req, res, next) {
    // jwt 判空
    if (!req.body.powerToken) {
        res.status(400).json({err: 'jwt为空'});
    }
    let powerToken = req.body.powerToken;
    // 验证jwt
    let result;
    let verifyJwtResult = await verifyJwtToken(powerToken);
    if (verifyJwtResult.login) {
        let uid = verifyJwtResult.data.uid;
        result = await userService.getUserInfoByUid(uid);
    } else {
        result = { success: false, msg: 'jwt无效', err: 'jwt无效' };
    }
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 用户完善个人信息
 */
router.post('/setUserInfo', async function(req, res, next) {
    // jwt 判空
    if (!req.body.powerToken) {
        res.status(400).json({err: 'jwt为空'});
    }
    let {
        powerToken, userIconId, username, phone, bio
    } = req.body;
    if (!username || !phone || !bio) {
        res.status(400).json({err: '传入参数有误'});
    }
    // 验证jwt
    let result;
    let verifyJwtResult = await verifyJwtToken(powerToken);
    if (verifyJwtResult.login) {
        let uid = verifyJwtResult.data.uid;
        result = await userService.setUserInfo(uid, userIconId, username, phone, bio);
    } else {
        result = { success: false, msg: 'jwt无效', err: 'jwt无效' };
    }
    res.status(result.success ? 200 : 400).json(result);
});

// 账号密码登录
router.post('/adminLogin', async function(req, res, next) {
    let { username, password } = req.body;
    if (!username || !password) {
        res.status(400).json({err: '传入参数有误'});
    }
    let result = await userService.adminLogin(username, password);
    res.status(result.success ? 200 : 400).json(result);
});

// 获取所有用户列表
router.get('/getAllUser', async function(req, res, next) {
    // 验证jwt
    let ptoken = req.headers.authorization;
    let page = parseInt(req.query.page || 1);
    let result;
    let verifyJwtResult = await verifyJwtToken(ptoken);
    if (verifyJwtResult.login) {
        let uid = verifyJwtResult.data.uid;
        result = await userService.getAllUser(uid, page);
    } else {
        result = { success: false, msg: 'jwt无效', err: 'jwt无效' };
    }
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;
